import { defineStore } from "pinia";
import { computed, reactive, ref, toRefs } from "vue";
import apis from '@/apis/index';

/**
 * 调薪记录-页面-共享数据仓库
 */
export const useSalaryAdjustmentStore = defineStore("salaryAdjustment", () => {
  const salaryAdjustmentListRef = ref([]);     // 职级列表
  const totalRef = ref(0);     // 列表长度
  const salaryAdjustmentDetailRef = ref(null);    // 职级详情
  const isLoadingRef = ref(true);   // 是否正在加载
  const isClosedRef = ref(false);   // 是否处于关闭状态
//   const isAttendanceLoadingRef = ref(true);
  
  // 更新职级列表
  async function updateSalaryAdjustmentList(data:any) {
    salaryAdjustmentListRef.value = data;
    totalRef.value = data.length;
  }

  // 更新职级详情
  async function updateSalaryAdjustmentDetail(data:any) {
    salaryAdjustmentDetailRef.value = data;
  }

  // 设置搜索栏状态
  function setIsClosed(bool: boolean) {
    isClosedRef.value = bool;
  }

  // 设置加载状态
  function setIsLoading(bool: boolean, delay: number = 500) {
    // setTimeout(() => {
      isLoadingRef.value = bool;
    // }, delay);
  }

  // 初始化职级列表
  async function initSalaryAdjustmentList(page = 1, size = 10, queryData = {}) {
    try {
      const params = {
        page,
        size,
        queryData,
      }
      const data = await apis.adjustSalary.getSalaryAdjustmentList_API(params);
      console.log(data,'SalaryAdjustment-list');
      if (data) {
        // 更新数据
        await updateSalaryAdjustmentList(data);
        console.log(salaryAdjustmentListRef,'SalaryAdjustmentListRef')
        setIsLoading(false);
        // 修改加载状态
        // setTimeout(() => {
        //   isAttendanceLoadingRef.value = false;
        //   setIsLoading(false);
        // }, 1500);
      }
    } catch (error) {
      console.log(error, 'pinia获取调薪记录列表数据失败');
      return;
    }
  }

  // 获取职级详情
  async function getSalaryAdjustmentDetail() {
    try {
      const data = await apis.adjustSalary.getSalaryAdjustmentDetail_API();
      console.log(data,'level-detail');
      if (data) {
        // 更新数据
        await updateSalaryAdjustmentDetail(data);
        console.log(salaryAdjustmentDetailRef,'levelDetailRef')
        // 修改加载状态
        // setTimeout(() => {
        //   isAttendanceLoadingRef.value = false;
        // }, 1500);
      }
    } catch (error) {
      console.log(error, 'pinia获取调薪记录详情数据失败');
      return;
    }
  }

  return {
    isClosedRef,
    setIsClosed,
    isLoadingRef,
    setIsLoading,
    salaryAdjustmentListRef,
    salaryAdjustmentDetailRef,
    totalRef,
    // isAttendanceLoadingRef,
    initSalaryAdjustmentList,
    getSalaryAdjustmentDetail
  }
});